<?php
namespace Home\Controller;
use Think\Controller;
class GuestEasyController extends CommonController {

	//新闻列表
    public function newsList(){

    	$user_id=$_SESSION['user']['id'];
    	$newsOrderModel=M('news_order');
    	$where['user_id']=$user_id;
        $where['news_target']='1';
    	$comList=getPages($newsOrderModel,$where);
  
        $this->assign("comList",$comList["comList"]);
        $this->assign("page",$comList["page"]);	

       $this->display();
    }
    public function orderDetail(){
    	$id=I('get.id'); 
    	$orderDetail=M('news_order')->where('id='.$id)->find();
    	if($orderDetail['type']=='1'){
    		$package=M('news_package')->where('id='.$orderDetail['news_ids'])->find();
    		$newsIds=M('news')->where(array('id'=>array('in',$package['package_content'])))->field('news_name,news_price')->select();
    		$orderDetail['package']=$package;
    		$orderDetail['newsIds']=$newsIds;
    	}else{
    		$newsIds=M('news')->where(array('id'=>array('in',$orderDetail['news_ids'])))->field('news_name,news_price')->select();
    		
    		$orderDetail['newsIds']=$newsIds;
    	}


    	$this->assign("orderDetail",$orderDetail);	
    	$this->display();
    }
    //自选新闻
    public function newsRelease(){

    	$newsModel=M('news');
    	$p = I('page') ? I('page') : 0;
    	$keyWord=I('keyword');
    	if($keyWord){

    	 	$where['news_name']=array("like","%".$keyWord."%");
    	 }

    	$pagesize=36;
    	$start =$p!=0?($p-1)*$pagesize:$p*$pagesize;  
 		$newsList=$newsModel->where($where)->field('id,news_name,news_price')->limit($start,$pagesize)->select();

      if($p>1 || $keyWord )
        {
               echo json_encode($newsList);
               
               exit;
        }else{
        	$sum =  $newsModel->count();
            $this->assign("totalPages",ceil($sum/C("PAGESIZE")));
        	$this->assign("comList",$newsList);
        	$this->assign("keyword",$keyWord);
        	$this->display();
        } 
       
    }
    //自选表单发布
    public function addNewsOrder(){

    	$map=I('post.');

    	if(!$map['news_ids'] || !$map['news_content']  ){

    		$this->error("","newsRelease");exit;
    	}

    	$data=array(
    		'user_id'=>$_SESSION['user']['id'],
    		'order_price'=>$map['order_price'],
    		'news_content'=>$map['news_content'],
    		'news_title'=>$map['news_title'],
    		'news_author'=>$_SESSION['user']['username'],
    		'news_ids'=>implode(",", $map['news_ids']),
            'news_target'=>'1',  
            'industry'=>implode(",", $map['industry']),
    		'add_time'=>date("Y-m-d H:i:s"),
    		);
    	
    	$res=M('news_order')->add($data);
    	if($res){
    		$this->success("","newsRelease");
    	}else{
    		$this->error("","newsRelease");
    	}


    }
    public function newsPackage(){

    	$packageList=M('news_package')->where('package_type=1')->select();
    	$newsModel=M('news');
    	foreach ($packageList as $key => $value) {
    		
    		$where['id']=array('in',$value['package_content']);
    		$packageList[$key]['package_content']=$newsModel->where($where)->field('news_name')->select();
    	}

    	$this->assign("packageList",$packageList);
    	$this->display();
    }
    //套餐表单发布
    public function addPackageOrder(){

    	$map=I('post.');
    	
    	if(!$map['news_ids'] || !$map['news_content']  ){

    		$this->error("","newsRelease");exit;
    	}

    	$data=array(
    		'user_id'=>$_SESSION['user']['id'],
    		'order_price'=>$map['order_price'],
    		'news_content'=>$map['news_content'],
    		'news_title'=>$map['news_title'],
    		'news_author'=>$_SESSION['user']['username'],
    		'news_ids'=> $map['news_ids'],
    		'add_time'=>date("Y-m-d H:i:s"),
    		'type'=>1
    		);
    	
    	$res=M('news_order')->add($data);
    	if($res){
    		$this->success("","newsPackage");
    	}else{
    		$this->error("","newsPackage");
    	}


    }
     //资源展示
    public function newsShow(){

        $keyWord=I('get.keyword');
    	if($keyWord){

    	 	$where['news_name']=array("like","%".$keyWord."%");
    	 }

    	 $newsModel=M('news');

    	$comList=getPages($newsModel,$where);
    	$this->assign("keyWord",$keyWord);
        $this->assign("comList",$comList["comList"]);
        $this->assign("page",$comList["page"]);	

       $this->display();
    }

}